<?php

/**
 * Hammer and Nails Software
 * Copyright (c) 2006-2007 Hammer and Nails Software. (http://www.hansoft.org)
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.hansoft.org/licenses/gpl-3.0.txt or through the GNU website:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@hansoft.org so we can send you a copy immediately.
 *
 * @copyright  (c) 2006-2007 Hammer and Nails Software. (http://www.hansoft.org)
 * @license    http://www.hansoft.org/licenses/gpl-3.0.txt
 * @version    $Id: UserController.php 6 2008-02-17 16:49:51Z philbsd $
 */

class UserController extends Hansoft_Controller_Action
{
    protected $_session;

    public function init()
    {
        $this->_session = new Zend_Session_Namespace(__CLASS__);
    }

    public function indexAction()
    {
        
    }

    public function loginAction()
    {
		if (isset($_POST['username'])) {
	        $options = array(
	            'missingMessage' => "Field '%field%' is required"
	            );

	        $filters = array(
	            'username' => array('StringTrim', 'StringToLower')
	            );

	        $validators = array(
	            'username' => array(
	                'presence' => 'required',
	                array('StringLength', 3, 32),
	                'Alpha',
	                'messages' => array(
	                    'Must be between %min% and %max% characters in length',
	                    'Must only contain alphabetic characters',
	                    ),
	                'breakChainOnFailure' => true
	                ),
	            'password' => array(
	                'presence' => 'required',
	                array('StringLength', 5),
	                'messages' => array(
	                    0 => 'Must be at least %min% characters in length'
	                    )
	                )
	            );

	        $input = new Zend_Filter_Input($filters, $validators, $_POST, $options);

	        if ($input->isValid()) {
	            $authAdapter = new Zend_Auth_Adapter_DbTable(
	                Zend_Registry::get('db'),
	                'users',
	                'username',
	                'password',
	                'MD5(?)'
	                );

	            $authAdapter->setIdentity($input->username)
	                        ->setCredential($input->password);

	            $auth = Zend_Auth::getInstance();

	            $result = $auth->authenticate($authAdapter);

	            if ($result->isValid()) {
	                $auth->getStorage()->write($authAdapter->getResultRowObject(null, 'password'));
	                unset($this->_session->messages, $this->_session->username);
	                $this->_redirect();
	            } else {
	                switch ($result->getCode()) {
	                    case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
	                        $messages = array('username' => array('No such identity exists'));
	                        break;
	                    case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
	                        $messages = array('password' => array('Does not match current password'));
	                        break;
	                    default:
	                        throw new Exception('Unsupported authentication failure code');
	                        break;
	                }
	                $this->_session->messages = $messages;
	                $this->_session->username = $_POST['username'];
	                $this->_redirect('user/login');
	            }

	        } else {
	            $this->_session->messages = $input->getMessages();
	            if (isset($_POST['username'])) {
	                $this->_session->username = $_POST['username'];
	            }
	            $this->_redirect('user/login');
	        }		
		}
		
		if (isset($this->_session->messages)) {
            $this->view->messages = $this->_session->messages;
        }
        if (isset($this->_session->username)) {
            $this->view->username = $this->_session->username;
        }		
        $this->render();
		unset($this->_session->messages, $this->_session->username);
    }
	
	public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect();
    }
}
